package top.yuyufeng.learn.lucene.solr.dao;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import top.yuyufeng.learn.lucene.solr.SolrServer;
import top.yuyufeng.learn.lucene.solr.core.BlogCore;

import java.awt.print.Pageable;
import java.util.List;
import java.util.Map;

/**
 * @author yuyufeng
 * @date 2017/12/6
 */
public class BlogSolrDao{
    HttpSolrClient server;

    public BlogSolrDao() {
        server = SolrServer.getServer();
    }

    /**
     * 按Bean  添加/修改 索引
     *
     * @throws Exception
     */
    public int addIndex(BlogCore entity) throws Exception {
        server.addBean(entity);
        UpdateResponse updateResponse = server.commit();
        return updateResponse.getStatus();
    }


    /**
     * 按Bean  添加/修改 索引
     *
     * @throws Exception
     */
    public int addIndexList(List<BlogCore> entitys) throws Exception {
        server.addBeans(entitys);
        UpdateResponse updateResponse = server.commit();
        return updateResponse.getStatus();

    }

    /**
     * 删除索引 按查询
     *
     * @throws Exception
     */
    public int deleteAll() throws Exception {
        String query = "*:*";
        server.deleteByQuery(query);
        server.commit();
        UpdateResponse updateResponse = server.commit();
        return updateResponse.getStatus();
    }

    /**
     * 删除索引 按id
     *
     * @throws Exception
     */
    public int deleteByQuery(Long id) throws Exception {
        server.deleteById(id + "");
        server.commit();
        UpdateResponse updateResponse = server.commit();
        return updateResponse.getStatus();
    }

    //查询索引
    public List<BlogCore> query() throws Exception {
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        query.setStart(0);//开始记录数
        query.setRows(10000);//总条数
        QueryResponse queryResponse = server.query(query);
        List<BlogCore> results = queryResponse.getBeans(BlogCore.class);
        return results;
    }


    //查询索引
    public  List<BlogCore> queryByKeyWords(String keywords) throws Exception {

        SolrQuery query = new SolrQuery();
        query.set("q", "keywords:" + keywords);//*通配多个字符
//        query.set("sort", "product_price desc");
        //======高亮设置===
        //开启高亮
        query.setHighlight(true);
        //高亮域
        query.addHighlightField("blogContent");
        query.addHighlightField("blogTitle");
        //前缀
        query.setHighlightSimplePre("<B>");
        //后缀
        query.setHighlightSimplePost("</B>");
        //query.setHighlightSnippets(1);//结果分片数，默认为1
        //query.setHighlightFragsize(1000);//每个分片的最大长度，默认为100

        query.setStart(0);//开始记录数
        query.setRows(100);//总条数
        QueryResponse queryResponse = server.query(query);
        long sum = queryResponse.getResults().getNumFound();
        List<BlogCore> results = queryResponse.getBeans(BlogCore.class);
        //输出高亮
        Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();

        for (BlogCore result : results) {
            Map<String, List<String>> map = highlighting.get(result.getBlogId()+"");
            List<String> list = map.get("blogTitle");
            if (list != null && list.size() > 0) {
                result.setBlogTitle(list.get(0));
            }
            list = map.get("blogContent");
            if (list != null && list.size() > 0) {
                result.setBlogContent(list.get(0));
            }
        }
       return results;
    }



}
